package com.xihuanjun.healthcode.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xihuanjun.healthcode.config.SysCache;
import com.xihuanjun.healthcode.dao.DownloadRecordDao;
import com.xihuanjun.healthcode.entity.DownloadRecord;
import com.xihuanjun.healthcode.entity.convert.ChangeRecordConvert;
import com.xihuanjun.healthcode.excel.ExcelRow;
import com.xihuanjun.healthcode.excel.ExcelRowListener;
import com.xihuanjun.healthcode.service.DownloadRecordService;
import com.xihuanjun.util.UploadUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

/**
 * (DownloadRecord)表服务实现类
 *
 * @author makejava
 * @since 2022-10-27 09:47:33
 */
@DependsOn("allTypes")
@Service("downloadRecordService")
public class DownloadRecordServiceImpl extends ServiceImpl<DownloadRecordDao, DownloadRecord> implements DownloadRecordService, InitializingBean {

    @Autowired
    ChangeRecordConvert changeRecordConvert;

    @Autowired
    UploadUtils uploadUtils;

    public void myinit(){
        QueryWrapper<DownloadRecord> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 1");
        DownloadRecord one = this.getOne(wrapper);
        SysCache.lastDownloadData=one;
        System.out.println(one);
        //读取
        String fileName = one.getDlFile();
        readExcelData(fileName);


        log.debug("全量数据读取完毕");
        log.debug("红数据："+SysCache.red.size());
        log.debug("橙数据："+SysCache.orange.size());
        log.debug("绿数据："+SysCache.green.size());
    }

    @Override
    public void readExcelData(String fileName) {
        File file=new File(uploadUtils.getSavePath()+File.separator+fileName);
        FileInputStream inputStream = null;
        try {
            inputStream = FileUtils.openInputStream(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        ExcelReader excelReader = EasyExcel.read(inputStream).build();

        ExcelRowListener listener1=new ExcelRowListener(changeRecordConvert,"红码",null,"red");
        ExcelRowListener listener2=new ExcelRowListener(changeRecordConvert,"橙码",null,"orange");
        ExcelRowListener listener3=new ExcelRowListener(changeRecordConvert,"绿码",null,"green");
        // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener
        ReadSheet readSheet1 =
                EasyExcel.readSheet(0).head(ExcelRow.class).registerReadListener(listener1).build();
        ReadSheet readSheet2 =
                EasyExcel.readSheet(1).head(ExcelRow.class).registerReadListener(listener2).build();
        ReadSheet readSheet3 =
                EasyExcel.readSheet(2).head(ExcelRow.class).registerReadListener(listener3).build();
        // 这里注意 一定要把sheet1 sheet2 一起传进去，不然有个问题就是03版的excel 会读取多次，浪费性能
        excelReader.read(readSheet1, readSheet2,readSheet3);
    }

    @Override
    public boolean newDownload(String fileName) {
        DownloadRecord dr=new DownloadRecord();
        dr.setDlFile(fileName);
        dr.setDlTime(new Date());

        dr.insert();
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        myinit();
    }
}

